/*
Version[0.0.0.1] UpdateTime[20221231] UsageDesc[增加假日判断接口]
Version[0.0.0.2] UpdateTime[20221231] UsageDesc[增加邮件导出接口]
Version[0.0.0.2] UpdateTime[20220102] UsageDesc[更新假期计算规则]
*/
package main

import (
	"flag"
	"fmt"

	"gitee.com/baal1990/gitee.com/baal1990/cmd/server_attence/app/config"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server_attence/app/global"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server_attence/app/middleware"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server_attence/app/router"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/cache"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/crypto"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/db"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/email"
	myzap "gitee.com/baal1990/gitee.com/baal1990/pkg/log/zap"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/msgcode"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/sms"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/util"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

var Version = "1.0.0.0"
var VersionUpdateTime = "20230113 13:00"
var UsageDesc = "外包考勤管理系统后台服务"

func main() {
	ver := flag.Bool("version", false, "specify config path")
	conf := flag.String("conf", "./server.yaml", "configuration")
	flag.Parse()

	if *ver {
		fmt.Println("Version    : ", Version)
		fmt.Println("UpdateTime : ", VersionUpdateTime)
		fmt.Println("Usage      : ", UsageDesc)
		return
	}
	// 初始化密码适配器
	global.Crypto = crypto.InitCryptoAdapter()

	// 加载配置
	if err := config.Load(*conf); err != nil {
		fmt.Println("invalid configuration!!!", err)
		return
	}
	// 初始化日志
	global.Log = myzap.Zap(myzap.LogConfig{
		Level:         config.Log().Level,
		Format:        config.Log().Format,
		Prefix:        config.Log().Prefix,
		Director:      config.Log().Director,
		LinkName:      config.Log().LinkName,
		ShowLine:      config.Log().ShowLine,
		EncodeLevel:   config.Log().EncodeLevel,
		StacktraceKey: config.Log().StacktraceKey,
		LogInConsole:  config.Log().LogInConsole,
	})

	global.Log.Info("ATTENCE START", zap.Any("VERSION", Version),
		zap.Any("UPDATETIME", VersionUpdateTime),
		zap.Any("USAGE", UsageDesc))

	//邮件配置初始化
	if err := email.Init(config.EMailSetting().Mailhost, config.EMailSetting().Mailport, config.EMailSetting().Mailuser, config.EMailSetting().Mailpwd); err != nil {
		global.Log.Fatal("初始化邮箱失败", zap.Any("err", err))
		return
	}
	// 初始化数据库
	if err := db.Init(config.Database().Driver, config.Database().Source, config.Database().Open, config.Database().Idle); err != nil {
		global.Log.Fatal("初始化数据库失败", zap.Any("err", err))
		return
	}
	// Redis初始化
	if err := cache.Init(config.Redis().Address, config.Redis().Password, 10, -1, config.Redis().Database); err != nil {
		global.Log.Fatal("初始化Redis失败", zap.Any("error", err))
		return
	}
	// SnowFlake初始化
	if err := util.InitSnowflake(config.SnowFlake().Node); err != nil {
		global.Log.Fatal("初始化雪花算法异常", zap.Any("err", err))
		return
	}
	// Sms初始化
	global.Sms.Init(sms.Params{
		AppKey:    config.SmsSetting().AppKey,
		AppSecret: config.SmsSetting().AppSecret,
		Host:      config.SmsSetting().Host,
	})
	// msgCodeGenerator初始化
	global.MsgCodeGenerator.Init(msgcode.Params{
		FreezeTime:     config.MsgcodeGenerator().MsgcodeFreezeTime,
		DurationCount:  config.MsgcodeGenerator().MsgcodeDurationCount,
		DurationWindow: config.MsgcodeGenerator().MsgcodeDurationWindow,
		ValidTime:      config.MsgcodeGenerator().MsgcodeValidTime,
		WrongTime:      config.MsgcodeGenerator().MsgcodeWrongTime,
	})
	// webserver初始化

	global.Log.Info("Server_Attence init finish,and start http service")

	r := gin.Default()
	r.SetTrustedProxies(config.Server().TrustedProxies) // 设置信任代理
	r.Use(middleware.WSSecurity())                      // ws-security校验
	r.Use(middleware.UriControl())                      // uri限流
	r.Use(middleware.ReqTimeLog())                      // 日志

	router.RouterCommon(r)
	router.Router2User(r)
	router.Router2Manager(r)

	global.Log.Info("Start Listening and serving HTTP.", zap.Any("port", config.Server().Port))
	r.Run(":" + config.Server().Port)
}
